package com.key.puzzlemodel.util

import android.view.View
import androidx.databinding.BindingAdapter

/**
 * 禁止快速点击
 */
@BindingAdapter("onNotQuickClick")
fun onNotQuickClick(view: View, onClickListener: View.OnClickListener? = null) {
    DoubleClickHelper.click(view, onClickListener)
}

/**
 * 快速点击
 */
@BindingAdapter("onQuickClick")
fun onQuickClick(view: View, onClickListener: View.OnClickListener? = null) {
    view.setOnClickListener(onClickListener)
}

object DoubleClickHelper {
    private const val duration = 300
    private var lastClickTime: Long = 0
    fun click(view: View, onClickListener: View.OnClickListener?) {
        view.setOnClickListener {
            val currentTime = System.currentTimeMillis()
            if (currentTime - lastClickTime > duration ||currentTime - lastClickTime <0) {
                lastClickTime = currentTime
                onClickListener?.onClick(view)
            }
        }
    }
}